/**
 *  编辑页
 *
 * 作者 zhangwei
 * 日期 2024-09-14 10:13:43
 */
layui.use(['jquery','febs', 'form', 'validate','treeSelect','commonJS'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        commonJS = layui.commonJS,
        treeSelect = layui.treeSelect,
        $view = $('#dept-edit'),
        validate = layui.validate;


    var parentId = POPUP_DATA.parentId;
    var tenantId = POPUP_DATA.tenantId;

    form.verify(validate);
    form.render();
    initFormValue();
    renderTree();
    function initFormValue() {
        if (deptData) {
            form.val("dept-edit-form", {
                "deptId": deptData.deptId,
                "parentId": deptData.parentId,
                "deptName": deptData.deptName,
                "deptCode": deptData.deptCode,
                "deptType": deptData.deptType,
                "businessCode": deptData.businessCode,
                "orderNum": deptData.orderNum,
                "status": deptData.status == '1'? true:false,
            });
        }
    }


    function renderTree()  {
        treeSelect.render({
            elem:'#parentId',
            data:ctx+'dept/treeSelect',
            type:'get',
            placeholder:'请选择上级部门',
            search:false,
            click:function (d) {
                $view.find('#parentId').val(d.current.id);
                $view.find('input[name="pid"]').val(d.current.id);
            },
            success:function (d) {
                if(deptData){
                    let parentId =  deptData.parentId;
                    if(!commonJS.isEmpty(parentId) && parentId != '0'){
                        treeSelect.checkNode("parentId",parentId)
                    }
                    $view.find('input[name="pid"]').val(parentId);
                }else{
                    if(!commonJS.isEmpty(parentId)){
                        treeSelect.checkNode("parentId",parentId);
                        $view.find('input[name="pid"]').val(parentId);
                    }
                }
            }

        })
    }


    form.on('submit(dept-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        let pid = data.pid;
        data.parentId = pid;
        if(data.status == 'on'){
            data.status = '1';
        }else{
            data.status = '0';
        }
        data.tenantId = tenantId;
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            if (data.deptId && data.deptId.length > 0) {
                febs.postArray(ctx + 'dept/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-deptNew').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'dept', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-deptNew').find('#query').trigger('click');
                });
            }
        })
    }

});